****************************************************************************************************************
**																											  **
** This do file is part of the replication material for the following article: 								  **
**  "Is There an Ideological Asymmetry in the Incumbency Effect? Evidence from U.S. Congressional Elections." **
** 		Authors: Morisi, Davide, Jost, John, Panagopoulos, Costas, and Jussi Valtonen		    			  **
** 		Journal: Social Psychological and Personality Science																			  **
**																											  **
** This file replicates the following step of the analysis: 												  **
**																											  **
**		RECODING CCES 2010 ORIGINAL DATASET			   													  	  **
**																											  **
****************************************************************************************************************

*Set working directory
cd "C:\Users\Davide Morisi\Dropbox\datasets\CCES"

*Open original dataset
*Stephen Ansolabehere, 2012, "CCES Common Content, 2010", https://doi.org/10.7910/DVN/VKKRWA, Harvard Dataverse, V3 
*Downloaded on January 16, 2020 from https://dataverse.harvard.edu/dataset.xhtml?persistentId=hdl:1902.1/17705
*usespss "2010\CCES_2010_CommonContent.sav"
*save "2010\CCES_2010_CommonContent.dta"
use "2010\cces_2010_common_validated.dta", clear

**************
*covariates

*gender
fre V208
rename V208 gender
*age
sum V207
rename V207 birthyr
*education
fre V213
rename V213 educ
*race
fre V211
recode V211 4/8=99, gen(racer) // 99=other
*religion
fre V219
recode V219 3/8=3 12=3 9=4 10=5 11=6, gen(religpewr) // note: atheist (9) missing
fre religpewr
*economy
fre CC302
rename CC302 economy
*state
fre V206
rename V206 state
clonevar state10 = state
*weight
sum V101
rename V101 weight1

*********************
*partisanship / ideology

*party of respondent - profile
*3 categories
*1 democrat, 2 republican, 3 independent, 4 other, 5 dk
fre V212a 
rename V212a pid3
*7 categories
*1 strong dem, 2 not strong dem, 3 lean dem, 4 indep, 5 lean rep, 6 not strong rep, 7 strong rep, 8 dk 
fre V212d 
rename V212d pid7
fre pid3 pid7
ta pid7 pid3 // leaning partisans coded as independent/other/dk

*party id post
*3 categories (note: cat 5 dk missing)
fre CC421a
rename CC421a pid3post
*5 categories
fre CC421b // leaners
rename CC421b leaners_prof_10
ta pid3post if leaners_prof_10!=. // asked only for independents/others
fre pid3post leaners_prof_10
gen pid5post = 1 if pid3post==1
replace pid5post = 2 if pid3post>2 & leaners_prof_10==1
replace pid5post = 3 if pid3post==3 & leaners_prof_10==3
replace pid5post = 4 if pid3post>2 & leaners_prof_10==2
replace pid5post = 5 if pid3post==2
*1 dem, 2 lean dem, 3 indep, 4 lean rep, 5 rep
fre pid5post


*ideology PROFILE
*1 very lib, 2 lib, 3 moderate, 4 cons, 5 very cons, 6 dk
fre V243
rename V243 ideo5

*ideology from PRE-ELECTION SURVEY
*1 very lib, 2 lib, 3 somewhat lib, 4 mod, 5 somewhat con, 6 con, 7 very con, 8dk
fre CC334A
rename CC334A ideo7


*****************
*vote post
*turnout
fre CC401
rename CC401 turnout10
clonevar turnout = turnout10

*senate
*fre CC355 // CC355 Senate Vote Intent (In the race for U.S. Senator in your state, who do you prefer?)
fre CC410a // Senator Vote - For whom did you vote for U.S. Senator?
*fre CC410b // Senator Vote Seat 2 (NY) - In the other race for U.S. Senator in your state, for whom did you vote?
fre CC410a
rename CC410a votesen10
*ta votesen10 CC410b, col
clonevar vote_sen = votesen10

*house
fre CC412
rename CC412 votehou10
clonevar vote_hou = votehou10


*****************
*contextual var - SENATE

*incumbent candidates
des V550_post V553_post
fre V550_post V553_post
clonevar SenCand1Incumbent_postr = V550_post
clonevar SenCand2Incumbent_postr = V553_post
label de incumb 0"not incumbent" 1"incumbent", modify
label values SenCand1Incumbent_postr incumb
label values SenCand2Incumbent_postr incumb
fre SenCand1Incumbent_postr // senate dem candidate incumbent
fre SenCand2Incumbent_postr // senate rep candidate incumbent

*party of candidate
fre V548_post // dem
fre V551_post // rep
encode V548_post, gen(V548_postr)
encode V551_post, gen(V551_postr)
gen SenCand1Party_postr = 1 if V548_postr!=.
gen SenCand2Party_postr = 2 if V551_postr!=.
label de partycand 1"dem. candidate" 2"rep. candidate" 3"other candidate", modify
label val SenCand1Party_postr partycand
label val SenCand2Party_postr partycand 
fre SenCand1Party_postr // dem cand
fre SenCand2Party_postr // rep cand
ta SenCand1Party_postr SenCand2Party_postr

*republican vs dem incumbent
gen inc_rep_sen = 0 if (SenCand1Party_postr==1 & SenCand2Party_postr==2) & (SenCand1Incumbent_postr==1 & SenCand2Incumbent_postr==0)
replace inc_rep_sen = 1 if (SenCand1Party_postr==1 & SenCand2Party_postr==2) & (SenCand1Incumbent_postr==0 & SenCand2Incumbent_postr==1)
fre inc_rep_sen // 0= dem inc (rep not inc), 1= rep inc (dem not inc)
*including districts with no dem/rep incumbents
ta SenCand1Incumbent_postr SenCand2Incumbent_postr if (SenCand1Party_postr==1 & SenCand2Party_postr==2)
gen inc_all_sen = inc_rep
recode inc_all_sen 0=-1
replace inc_all_sen = 0 if (SenCand1Party_postr==1 & SenCand2Party_postr==2) & (SenCand1Incumbent_postr==0 & SenCand2Incumbent_postr==0)
ta inc_all_sen // -1=dem inc, 0=inc not dem nor rep, 1=rep inc

*keep only states with elections, not open elections
ta state inc_rep_sen, nol
*filter
/*gen count2=1 if inc_rep!=.
egen count_sum2 = sum(count2), by(state)
gen filter_sen2=1 if inc_rep!=.
replace filter_sen2=2 if count_sum2<10 & inc_rep!=.
replace filter_sen2=3 if (state==16 | state==37 | state==40) & inc_rep==0
replace filter_sen2=3 if (state==15 | state==36 | state==53) & inc_rep==1
ta state inc_rep if filter_sen2==1*/
ta V548_postr //dem candidate name
ta V551_postr // rep candidate name
bys state: ta V548_postr if inc_rep_sen!=.
gen count=1 if inc_rep_sen!=.
egen count_sum = sum(count), by(state V548_postr)
gen filter_sen=1 if inc_rep_sen!=.
replace filter_sen=2 if count_sum<10 & inc_rep_sen!=.
ta state inc_rep_sen
ta state inc_rep_sen if filter_sen==1
clonevar filter_sen10 = filter_sen

/*ta V548_postr V551_postr if state==32 & inc_rep!=.
ta V548_postr V551_postr if state==32 & inc_rep!=. & filter_sen2==1
ta V548_postr V551_postr if state==32 & inc_rep!=. & filter_sen==1 // the new filter works better!*/

***
*contextual var HOUSE

*incumbent candidates
des V535_post V538_post
fre V535_post V538_post
encode V535_post, gen(HouseCand1Incumbent_postr)
encode V538_post, gen(HouseCand2Incumbent_postr)
fre HouseCand1Incumbent_postr HouseCand2Incumbent_postr
recode HouseCand1Incumbent_postr 1=0 2=1
recode HouseCand2Incumbent_postr 1=0 2=1
label val HouseCand1Incumbent_postr incumb
label val HouseCand2Incumbent_postr incumb
fre HouseCand1Incumbent_postr HouseCand2Incumbent_postr
ta HouseCand1Incumbent_postr HouseCand2Incumbent_postr

*party of candidate
fre V533 // dem candidate
fre V536 // rep candidate
encode V533, gen(V533r)
encode V536, gen(V536r)
gen HouseCand1Party_postr = 1 if V533r!=.
gen HouseCand2Party_postr = 2 if V536r!=.
label val HouseCand1Party_postr partycand
label val HouseCand2Party_postr partycand
fre HouseCand1Party_postr HouseCand2Party_postr
ta HouseCand1Party_postr HouseCand2Party_postr

*republican incumbent vs. dem incumbent
*note: with this var we restrict analysis to seats with dem vs. rep AND with inc. vs. not inc.
fre HouseCand1Party_postr HouseCand1Incumbent_postr
fre HouseCand2Party_postr HouseCand2Incumbent_postr
gen inc_rep_hou = 0 if (HouseCand1Party_postr==1 & HouseCand2Party_postr==2) ///
& (HouseCand1Incumbent_postr==1 & HouseCand2Incumbent_postr==0)
replace inc_rep_hou = 1 if (HouseCand1Party_postr==1 & HouseCand2Party_postr==2) ///
& (HouseCand1Incumbent_postr==0 & HouseCand2Incumbent_postr==1)
fre inc_rep_hou
ta HouseCand1Incumbent_postr HouseCand2Incumbent_postr if inc_rep_hou!=.
*including districts with no dem/rep incumbents
ta HouseCand1Incumbent_postr HouseCand2Incumbent_postr if (HouseCand1Party_postr==1 & HouseCand2Party_postr==2) 
gen inc_all_hou = inc_rep_hou
recode inc_all_hou 0=-1
replace inc_all_hou = 0 if (HouseCand1Party_postr==1 & HouseCand2Party_postr==2) & (HouseCand1Incumbent_postr==0 & HouseCand2Incumbent_postr==0)
fre inc_all_hou // -1=dem inc, 0=inc not dem nor rep, 1=rep inc

*keep only states with elections, not open elections
ta state inc_rep_hou
*filter
gen count1=1 if inc_rep_hou!=.
egen count_sum1 = sum(count1), by(state V533r)
gen filter_hou=1 if inc_rep_hou!=.
replace filter_hou=2 if count_sum1<10 & inc_rep_hou!=.
ta state inc_rep_hou if filter_hou==1
*problem with connecticut
tab V533 inc_rep_hou if state==9 // murphy coded also as republican incumbent
drop if V533=="Christopher Murphy" & state==9 & inc_rep_hou==1
*double check
bys state: ta V533 if filter_hou==1
clonevar filter_hou10 = filter_hou


*******************
*combined variables > vote + incumbent status

*vote + incumbent status, all options - SENATE
ta vote_sen inc_all_sen
fre turnout vote_sen
fre inc_all_sen // -1=dem inc, 0=inc not dem nor rep, 1=rep inc
gen votinc_sen_all = 0 if turnout<5 | vote_sen==5 | vote_sen==6 // did not vote
replace votinc_sen_all = 10 if vote_sen==1 & inc_all_sen==-1
replace votinc_sen_all = 11 if vote_sen==1 & inc_all_sen==0
replace votinc_sen_all = 12 if vote_sen==1 & inc_all_sen==1
replace votinc_sen_all = 20 if vote_sen==2 & inc_all_sen==-1
replace votinc_sen_all = 21 if vote_sen==2 & inc_all_sen==0
replace votinc_sen_all = 22 if vote_sen==2 & inc_all_sen==1
*label var votinc_sen_all "Voted for dem/rep by incumbent status - SENATE"
*label de votinc_sen_all 0"did not vote" 10"vote dem, dem inc"  11"vote dem, no inc"  12"vote dem, rep inc" ///
*20"vote rep, dem inc"  21"vote rep, no inc"  22"vote rep, rep inc" 
*label val votinc_sen_all votinc_sen_all
fre votinc_sen_all

*vote + incumbent status, all options - HOUSE
ta vote_hou inc_all_hou
fre vote_hou
fre inc_all_hou // -1=dem inc, 0=inc not dem nor rep, 1=rep inc
gen votinc_hou_all = 0 if turnout<5 | vote_hou==8 | vote_hou==9 // did not vote
replace votinc_hou_all = 10 if vote_hou==1 & inc_all_hou==-1
replace votinc_hou_all = 11 if vote_hou==1 & inc_all_hou==0
replace votinc_hou_all = 12 if vote_hou==1 & inc_all_hou==1
replace votinc_hou_all = 20 if vote_hou==2 & inc_all_hou==-1
replace votinc_hou_all = 21 if vote_hou==2 & inc_all_hou==0
replace votinc_hou_all = 22 if vote_hou==2 & inc_all_hou==1
*label var votinc_hou_all "Voted for dem/rep by incumbent status - HOUSE"
*label val votinc_hou_all votinc_sen_all
fre votinc_hou_all


***
gen year = 2010

rename V100 caseid10
clonevar caseid = caseid10

fre turnout
gen tookpost = 1 if turnout!=.

****
*create congressional districts
decode state, gen(stated)
drop if stated=="District of Columbia"
encode stated, gen(staten)
label var staten "States from 1 to 50 in alphabetical order"
fre staten // alphabetical order, from 0 to 50

*combine with district
rename V276_post cd2010str
encode cd2010str, gen(cd2010)
*probl: states with one district, district coded as 00 instead of 01
*single district states: alaska, delaware, montana, north dakota, south dakota, vermont, wyoming
fre staten if cd2010==1
gen single_district = 1 if staten==2 | staten==8 | staten==26 | staten==34 | staten==41 | staten==45 | staten==50
fre staten if single_district==1

gen cd2010str_correct = cd2010str if single_district==.
replace cd2010str_correct="01" if single_district==1 & cd2010!=.
label var cd2010str_correct "Post  Congressional district 2010 00=01"
fre cd2010str cd2010str_correct if single_district==1

egen stcd=concat(staten cd2010str_correct)

*double check > all correct
fre stcd if staten==9 // florida
fre stcd if staten==9 & cd2010!=.
fre stcd if staten==32 // ny
fre stcd if staten==32 & cd2010!=.
fre stcd if staten==45 // vermont
fre stcd if staten==45 & cd2010!=.
fre stcd if staten==50 // wyoming
fre stcd if staten==50 & cd2010!=.

*
rename stcd stcd_string
gen stcd = real(stcd_string) // turn into numeric
label var stcd_string "State (1 to 50 alphabetical) + congr. district (01 to n) STRING"
label var stcd "State (1 to 50 alphabetical) + congr. district (01 to n)"

***
/*merge with contextual var

save "CCES_2010_recoded2.dta", replace
import excel "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Other files\hr4618.xls", sheet("2010") firstrow clear
sum year stcd inc pwin dv dvp fr po1 po2 redist dexp rexp dpres switchb switcha
drop year switchb switcha
save "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Replication do files\Data\house_data_2010.dta", replace
use "CCES_2010_recoded2.dta", clear
*/

*merge info about previous election
merge m:1 stcd using "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Replication do files\Data\house_data_2008.dta" // all matched from using
drop pwin dv dvp fr redist dexp rexp dpres _merge
rename inc inc_previous
rename po1 po1_previous
rename po2 po2_previous

*post election
merge m:1 stcd using "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Replication do files\Data\house_data_2012.dta" // all matched
drop pwin dv dvp fr redist dexp rexp dpres _merge
rename inc inc_post
rename po1 po1_post
rename po2 po2_post

merge m:1 stcd using "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Replication do files\Data\house_data_2010.dta" // all matched from using
drop _merge

***
*voter registration status post
fre V203_post
recode V203_post (2 3=0 "No/DK") (1=1 "Yes"), gen(register_post)
label var register_post "Registered to vote? (post)"
fre register_post

***
*job approval
*Please indicate whether you approve or disapprove of the job that each of the following are doing.
* 1 Strongly approve
* 2  Approve
* 3 Disapprove
* 4 Strongly disapprove
* 5 never heard

*House representative
fre CC315a 
rename CC315a approval_house

*senate
*2 senators for each state. 
fre CC315b CC315c 
rename CC315b approval_sen1
rename CC315c approval_sen2

***
*keep only selected variables
keep gender birthyr educ racer religpewr state state10 weight1 economy ///
pid3 pid7 pid3post pid5post leaners_prof_10 ///
ideo5 ideo7 ///
turnout turnout10 vote_sen votesen10 vote_hou votehou10 ///
SenCand1Incumbent_postr SenCand2Incumbent_postr SenCand1Party_postr SenCand2Party_postr ///
inc_rep_sen inc_all_sen filter_sen filter_sen10 ///
HouseCand1Incumbent_postr HouseCand2Incumbent_postr HouseCand1Party_postr HouseCand2Party_postr ///
inc_rep_hou inc_all_hou filter_hou filter_hou10 ///
votinc_sen_all votinc_hou_all ///
year caseid caseid10 tookpost ///
staten ///
cd2010 cd2010str cd2010str_correct single_district stcd_string stcd ///
inc pwin dv dvp fr po1 po2 redist dexp rexp dpres ///
register_post approval_house approval_sen1 approval_sen2 ///
inc_previous po1_previous po2_previous ///
inc_post po1_post po2_post

*save recoded file
save "CCES_2010_recoded2.dta", replace
